# -*- coding: utf-8 -*-

from datetime import timedelta
from utils.alerts.yl_threeSegCodeOnFailue import yl_threeSegCodeOnFailure
from utils.alerts.yl_threeSegCodeOnSuccess import yl_threeSegCodeOnSuccess
from utils.operators.cluster_for_spark_sql_operator import SparkSqlOperator

from jms.dwd.oms.dwd_yl_oms_oms_waybill_incre_dt import jms_dwd__dwd_yl_oms_oms_waybill_incre_dt
from jms.dwd.oms.dwd_oms_waybill_overtime_dt import jms_dwd__dwd_oms_waybill_overtime_dt
from jms.dim.dim_lmdm_sys_tail_code import jms_dim__dim_lmdm_sys_tail_code
from jms.dim.dim_lmdm_sys_tail_code_staff import jms_dim__dim_lmdm_sys_tail_code_staff
from jms.dwd.tab.dwd_tab_barscan_deliver_base_dt import jms_dwd__dwd_tab_barscan_deliver_base_dt
from jms.aigroup.ai_dwd.cn_mysql_lmdm_sys_network_electronic_fence_change import jms_ai_dwd__cn_mysql_lmdm_sys_network_electronic_fence_change
from jms.dwd.oms.dwd_yl_oms_interceptorpiece_base_dt import jms_dwd__dwd_yl_oms_interceptorpiece_base_dt
from jms.dwd.tab.dwd_tab_end_piece_base_dt import jms_dwd__dwd_tab_end_piece_base_dt
from jms.dwd.tab.dwd_tab_barscan_difficult_base_dt import jms_dwd__dwd_tab_barscan_difficult_base_dt
from jms.dim.dim_lmdm_sys_network import jms_dim__dim_lmdm_sys_network

def kwargs():
    kwargs = {
        "db": "ai_dwd",
        "table": "yl_ml_clean_address_day",
        "desc": "全国日更新地址数据",
        "taskid": "10040",
        "ifprivacy": 0,
        "warnignore": 0,
    }
    return kwargs

jms_ai_dwd__yl_ml_clean_address_day = SparkSqlOperator(
    task_id='jms_ai_dwd__yl_ml_clean_address_day',
    conn_id='spark_default',
    email=['yushuo@jtexpress.com','yl_bigdata@yl-scm.com'],
    pool_slots=2,
    # depends_on_past=True,  # 如果任务依赖于前一天的同名任务，则将 depends_on_past 设为 True
    task_concurrency=1,  # 如果任务不支持并发，则将 task_concurrency 设为 1
    name='jms_ai_dwd__yl_ml_clean_address_day_{{ execution_date | date_add(1) | cst_ds }}',  # yarn 任务名称
    yarn_queue='pyspark',
    sql=f'jms/aigroup/ai_dwd/yl_ml_clean_address_day/execute.sql',
    driver_memory='4G',
    executor_memory='12G',
    executor_cores=6,
    num_executors=20,
    conf={'spark.dynamicAllocation.enabled': 'true',  # 动态资源开启
          'spark.shuffle.service.enabled': 'true',  # 动态资源 Shuffle 服务开启
          'spark.dynamicAllocation.maxExecutors': 40,  # 动态资源最大扩容 Executor 数
          'spark.dynamicAllocation.cachedExecutorIdleTimeout': 120,  # 动态资源自动释放闲置 Executor 的超时时间(s)
          'spark.sql.sources.partitionOverwriteMode': 'dynamic',  # 允许删改已存在的分区
          'spark.executor.memoryOverhead': '1G',  # 堆外内存
          'spark.sql.shuffle.partitions': 800,
          },
    hiveconf={'hive.exec.dynamic.partition': 'true',  # 动态分区
              'hive.exec.dynamic.partition.mode': 'nonstrict',
              'hive.exec.max.dynamic.partitions': 200,
              'hive.exec.max.dynamic.partitions.pernode': 200,
              },
    on_success_callback=yl_threeSegCodeOnSuccess(kwargs(), dingding_conn_id="dingding_ThreeSeg_etl_info"),
    on_failure_callback=yl_threeSegCodeOnFailure(kwargs(), dingding_conn_id="dingding_ThreeSeg_etl_alert"),

)

# 设置依赖
jms_ai_dwd__yl_ml_clean_address_day << [

jms_dwd__dwd_yl_oms_oms_waybill_incre_dt
,jms_dwd__dwd_oms_waybill_overtime_dt
,jms_dim__dim_lmdm_sys_tail_code
,jms_dim__dim_lmdm_sys_tail_code_staff
,jms_dwd__dwd_tab_barscan_deliver_base_dt
,jms_ai_dwd__cn_mysql_lmdm_sys_network_electronic_fence_change
,jms_dwd__dwd_yl_oms_interceptorpiece_base_dt
,jms_dwd__dwd_tab_end_piece_base_dt
,jms_dwd__dwd_tab_barscan_difficult_base_dt
,jms_dim__dim_lmdm_sys_network
  ]

